package user.zc.action;

import user.zc.action.comp.SaveOrUpdateAction;
import user.zc.controller.ISystemTemplate;
import user.zc.vo.ColumnDTO;

import javax.swing.*;
import java.util.Map;
import java.util.function.Consumer;

public class AddOrUpdateSystemTemplateAction extends SaveOrUpdateAction<String,Boolean> {

    public AddOrUpdateSystemTemplateAction(ISystemTemplate systemTemplateController, Consumer<Boolean> callback){
        this(systemTemplateController,null,null,callback);
    }
    public AddOrUpdateSystemTemplateAction(ISystemTemplate systemTemplateController, String name,String context, Consumer<Boolean> callback){
        super(callback,context,650,700);
        try {
            addItem("名称","name",true, InputType.MediumInput,name);
            addItem("内容","content",true,InputType.ContentType,context);
            openDialog(name!=null?("修改【"+name+"】模板"):"新增模板",(dto)->{
                try {
                    systemTemplateController.saveOrUpdate(mapping().get("name"),dto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JOptionPane.showMessageDialog(null,"保存成功");
                call(true);
            });
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                    e.getMessage(), "错误提示", JOptionPane.ERROR_MESSAGE);
        }
    }


    @Override
    protected String map2Entity(Map<String, String> map) {
        return map.get("content");
    }
}
